package com.xh.web.controller;


import java.io.IOException;
import java.util.*;

import com.xh.web.constant.AjaxResult;
import com.xh.web.domain.DColumn;
import com.xh.web.domain.DTable;
import com.xh.web.domain.vo.ColumnVo;
import com.xh.web.service.IDColumnService;
import com.xh.web.service.IDProjectService;
import com.xh.web.service.IDTableService;
import com.xh.web.util.StringUtils;
import com.xh.web.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 字段Controller
 *
 * @author dongxuefei
 * @date 2020-09-04
 */
@RestController
@RequestMapping("/db/column")
public class DColumnController extends BaseController
{
    @Autowired
    private IDColumnService dColumnService;
    @Autowired
    private IDTableService dTableService;
    @Autowired
    private IDProjectService dProjectService;

    /**
     * 查询字段列表
     */
    @GetMapping("/selectAllColumn")
    public TableDataInfo list(DColumn column)
    {
        startPage();
        List<DColumn> list = dColumnService.selectDColumnList(column);
        return getDataTable(list);
    }


    /**
     * 查询表下的索引列
     *
     * @return
     */
    @PostMapping("/selectIndexColumn")
    public TableDataInfo selectByCode( @RequestBody DTable dTable) {
       DTable dTable1 = dTableService.selectDTableByCode(dTable.getTableCode(),dTable.getProjectId());
       DColumn  dColumn=new DColumn();
       dColumn.setTableId(dTable1.getId());
        List<DColumn> list = dColumnService.selectDColumnList(dColumn);
        return getDataTable(list);
    }


    /**
     * 新增字段
     */
    @PostMapping("/insertColumn")
    public AjaxResult add(@RequestBody DColumn dColumn)
    {
       DColumn dColumn1= dColumnService.selectColumnByCode(dColumn.getColumnCode(),dColumn.getTableId());
            if (dColumn1 !=null){
                return AjaxResult.error("字段代码已存在");
            }
        return toAjax(dColumnService.insertDColumn(dColumn));
    }

    /**
     * 保存从模板选择的字段
     */
    @PostMapping("/insertChoiceColumn")
    @ResponseBody
    public AjaxResult addList(@RequestBody DColumn dColumn)
    {
        return toAjax(dColumnService.insertChoiceDColumn(dColumn.getIds(),dColumn.getTableId()));
    }


    /**
     * 修改字段
     */
    @PutMapping("/updateColumn")
    public AjaxResult edit(@RequestBody DColumn dColumn)
    {
        if(StringUtils.isNotNull(dColumn.getColumnCode())){
            DColumn dColumn1=dColumnService.selectColumnById(dColumn.getId());
            DColumn column= dColumnService.selectColumnByCode(dColumn.getColumnCode(),dColumn1.getTableId());
            if (!dColumn.getColumnCode().equals(dColumn1.getColumnCode())){
                if (column !=null){
                    return AjaxResult.error("字段代码已存在");
                }
            }
        }

        return toAjax(dColumnService.updateDColumn(dColumn));
    }

    /**
     * 删除字段
     */
	@DeleteMapping("delColumn/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(dColumnService.deleteDColumnById(id));
    }

    /**
     * 预览sql
     */
    @GetMapping("/previewColumn/{id}")
    public AjaxResult preview(@PathVariable("id") String id) throws IOException
    {
        Map<String, String> dataMap = dColumnService.previewCode(id);
        return AjaxResult.success(dataMap);
    }

    /**
     * 根据projectId查询column
     */
    @GetMapping("/selectColumnByProjectId")
    public AjaxResult selColumnByProjectId(ColumnVo columnVo) {
        return AjaxResult.success(dColumnService.selectDProjectById(columnVo));
    }

    /**
     * 加入模板
     */
    @PostMapping("/joinTemplate")
    public AjaxResult joinTemplate(@RequestBody DColumn dColumn)
    {
        return toAjax(dColumnService.joinTemplate(dColumn));
    }


}
